/*
 * ENUM Discoverer. ENUM Demonstration software, demonstrating the potential of ENUM.
 * 
 * Copyright (C) 2009 SIDN and ISOC.nl
 * 
 * This file is part of ENUM Discoverer.
 *
 * ENUM Discoverer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ENUM Discoverer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ENUM Discoverer.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.sidn.enumdiscoverer.callhandling;

import nl.sidn.enumdiscoverer.LogConfig;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * Receives broadcasts of incoming and outgoing calls and displays our replacement in-call
 * screen in front of the system default one.
 * 
 * @author	Grant Patterson (grant.patterson@catalyst.net.nz)
 */
public class CallBroadcastReceiver extends BroadcastReceiver {

	static final String TAG = LogConfig.getLogTag(CallBroadcastReceiver.class);
	// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
	static final boolean DEBUG = LogConfig.isDebug(TAG);
	// whether VERBOSE level logging is enabled
	static final boolean VERBOSE = LogConfig.VERBOSE;
	
	@Override
	public void onReceive(Context context, Intent intent) {
		
		if (VERBOSE) Log.v(TAG, "Recieved intent: " + intent);
		
		String action = intent.getAction();
		
		final String number;
		final boolean isInbound;
		
		
		if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) {
			
			isInbound = false;
			number = getResultData();
			
		} else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
			
			// we are only interested in handling new (ie RINGING) incoming calls through this action.
			// (Intent.ACTION_NEW_OUTGOING_CALL is better for outgoing calls)
			
			String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
			number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
			
			if (!TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
				// we're not interested
				if (VERBOSE) Log.v(TAG, "Not a new incoming call: state="+state + ", number="+number);
				return;
			}
			
			// assume that only incoming calls can enter the RINGING state
			
			if (DEBUG) Log.d(TAG, "New incoming call: state="+state + ", number="+number);
			
			isInbound = true;
			
		} else {
			Log.i(TAG, "Unexpected action: " + action);
			return;
		}
		
		if (number == null) {
			if (DEBUG) Log.d(TAG, "Couldn't determine a number for the call. Nothing to do.");
			return;
		}
		

		Intent i = new Intent(context, CallHandlingService.class);
		i.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
		i.putExtra(CallHandlingService.DIRECTION,
				isInbound ? CallHandlingService.DIRECTION_INCOMING :
					CallHandlingService.DIRECTION_OUTGOING);
		context.startService(i);
		
	}

}
